package com.berwin.caper.view;

import com.berwin.caper.entity.Tag;
import com.berwin.caper.view.components.FileTreePanel;
import com.berwin.caper.view.components.IFileTreeAdapter;
import com.berwin.caper.view.components.ItemTagView;
import com.berwin.caper.view.components.filetree.JFileTree;
import com.berwin.common.utils.Utility;

import javax.swing.*;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.tree.*;
import java.awt.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainView extends JFrame {

    private JLabel lblStatus;

    public MainView() {
        try {
            /**
             * 设置图形界面外观 java的图形界面外观有3种,默认是java的金属外观,还有就是windows系统,motif系统外观.
             * 1、Metal风格 (默认) UIManager.setLookAndFeel(
             * "javax.swing.plaf.metal.MetalLookAndFeel"); 2、Windows风格
             * UIManager.setLookAndFeel(
             * "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 3、Windows
             * Classic风格 UIManager.setLookAndFeel(
             * "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
             * 4、Motif风格 UIManager.setLookAndFeel(
             * "com.sun.java.swing.plaf.motif.MotifLookAndFeel"); 5、Mac风格
             * (需要在相关的操作系统上方可实现) String lookAndFeel =
             * "com.sun.java.swing.plaf.mac.MacLookAndFeel"
             * ;UIManager.setLookAndFeel(lookAndFeel); 6、GTK风格 (需要在相关的操作系统上方可实现)
             * String lookAndFeel =
             * "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
             * UIManager.setLookAndFeel(lookAndFeel); 7、可跨平台的默认风格 String
             * lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
             * UIManager.setLookAndFeel(lookAndFeel); 8、当前系统的风格 String
             * lookAndFeel = UIManager.getSystemLookAndFeelClassName();
             * UIManager.setLookAndFeel(lookAndFeel);
             */
            if ("Mac OS X".equals(System.getProperties().getProperty("os.name"))) {
                UIManager
                        .setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//                System.setProperty("apple.laf.useScreenMenuBar", "true");//设置程序的menubar显示到Mac的菜单栏上
                System.setProperty("com.apple.mrj.application.apple.menu.about.name", "BMFont工具");//设定Mac中程序运行时菜单栏显示
            } else {
                UIManager
                        .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        this.setLayout(new BorderLayout());
        this.setSize(Utility.getScreenSize(0.8f, 0.8f));
        this.setTitle("Caper SFTP");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);

//        this.initJFileTreeView();
//        this.initTestTreeView();
        this.initMenuBar();
        this.initContent();
        this.initStatusBar();
    }

    private void initJFileTreeView() {
        JFileTree fileTree = new JFileTree("/Users/berwin/Documents/XJGKS/Workspaces/Doudizhu/build");
        JScrollPane scrollPane = new JScrollPane(fileTree);
        this.add(scrollPane, BorderLayout.CENTER);
    }

    private void initTestTreeView() {
        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("我的电脑");

        DefaultMutableTreeNode diskCNode = new DefaultMutableTreeNode("User");
        DefaultMutableTreeNode diskDNode = new DefaultMutableTreeNode("D:");
        DefaultMutableTreeNode diskENode = new DefaultMutableTreeNode("E:");

        rootNode.add(diskCNode);
        rootNode.add(diskDNode);
        rootNode.add(diskENode);

        DefaultMutableTreeNode diskCChild1Node = new DefaultMutableTreeNode("User");
        DefaultMutableTreeNode diskCChild2Node = new DefaultMutableTreeNode("System");
        DefaultMutableTreeNode diskCChild3Node = new DefaultMutableTreeNode("Documents");
        DefaultMutableTreeNode diskCChild4Node = new DefaultMutableTreeNode("Downloads");

        diskCNode.add(diskCChild1Node);
        diskCNode.add(diskCChild2Node);
        diskCNode.add(diskCChild3Node);
        diskCNode.add(diskCChild4Node);

        JTree tree = new JTree(rootNode);
        tree.setEditable(true);
        tree.addTreeWillExpandListener(new TreeWillExpandListener() {
            @Override
            public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
                System.out.println("treeWillExpand:" + _treePathsToFilePath(event.getPath().getPath()));
            }

            @Override
            public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {
                System.out.println("treeWillCollapse");
            }
        });
        tree.addTreeSelectionListener(new TreeSelectionListener() {
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                Object a = e.getSource();
                System.out.println("当前被选中的节点: " + e.getPath());
            }
        });

        JScrollPane scrollPane = new JScrollPane(tree);
        this.add(scrollPane, BorderLayout.CENTER);
    }


    private String _treePathsToFilePath(Object[] treePaths) {
        String result = "";
        for (int i = 0; i < treePaths.length; i++) {
            result += File.separator + ((TreePath) treePaths[i]).getPath();
        }
        return result;
    }

    private void initContent() {
        FileTreePanel fileTreePanel1 = new FileTreePanel(new IFileTreeAdapter() {
            @Override
            public List<Tag> getTags() {
                List<Tag> tags = new ArrayList<>();
                tags.add(new Tag("/Users/berwin/Documents/XJGKS"));
                tags.add(new Tag("/Users/berwin/Documents/Workspace"));
                return tags;
            }

            @Override
            public ItemTagView getItemTagView(JPanel parent, int idx, Tag data) {
                ItemTagView itemTagView = new ItemTagView(data, itemTagView1 -> {
                    System.out.println("left:" + idx);
                });
                return itemTagView;
            }

            @Override
            public String getDefaultPath() {
                return "/Users/berwin/Documents/XJGKS/Workspaces/Doudizhu/build";
            }
        });
        FileTreePanel fileTreePanel2 = new FileTreePanel(new IFileTreeAdapter() {
            @Override
            public List<Tag> getTags() {
                List<Tag> tags = new ArrayList<>();
                tags.add(new Tag("/Users/berwin/Documents/XJGKS"));
                tags.add(new Tag("/Users/berwin/Documents/Workspace"));
                tags.add(new Tag("/Users/berwin/Documents/XJGKS"));
                tags.add(new Tag("/Users/berwin/Documents/Workspace"));
                tags.add(new Tag("/Users/berwin/Documents/XJGKS"));
                tags.add(new Tag("/Users/berwin/Documents/Workspace"));
                tags.add(new Tag("/Users/berwin/Documents/XJGKS"));
                tags.add(new Tag("/Users/berwin/Documents/Workspace"));
                return tags;
            }

            @Override
            public ItemTagView getItemTagView(JPanel parent, int idx, Tag data) {
                ItemTagView itemTagView = new ItemTagView(data, itemTagView1 -> {
                    System.out.println("right:" + idx);
                });
                return itemTagView;
            }

            @Override
            public String getDefaultPath() {
                return "/Users/berwin/Documents/XJGKS/Workspaces/Doudizhu/build";
            }
        });

        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, fileTreePanel1, fileTreePanel2);
        splitPane.setSize(this.getWidth(), this.getHeight());
        splitPane.setDividerSize(4);
        splitPane.setEnabled(false);
        splitPane.setDividerLocation(0.5f);
        this.add(splitPane, BorderLayout.CENTER);
    }

    private void initMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        menuBar.setBorder(null);
        this.setJMenuBar(menuBar);

        JMenu fileMenu = new JMenu("文件");
        menuBar.add(fileMenu);

        JMenu editMenu = new JMenu("编辑");
        menuBar.add(editMenu);

        JMenu tagMenu = new JMenu("书签");
        menuBar.add(tagMenu);

        JMenu srvMenu = new JMenu("服务器");
        menuBar.add(srvMenu);
        srvMenu.addSeparator();

        JMenuItem addSrvItem = new JMenuItem("添加服务器");
        srvMenu.add(addSrvItem);
    }

    private void initStatusBar() {
        JPanel pnlSouth = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        this.add(pnlSouth, BorderLayout.SOUTH);
        lblStatus = new JLabel("链接成功");
        Font font = new Font(this.lblStatus.getFont().getName(), Font.ITALIC, 10);
        lblStatus.setFont(font);
        pnlSouth.add(lblStatus);

        JLabel lblPoint = new JLabel(new ImageIcon("./res/point.png"));
        lblPoint.setForeground(Color.GREEN);
        lblPoint.setBackground(Color.GREEN);
        pnlSouth.add(lblPoint);
    }

    public void updateStatus(String text) {
        this.lblStatus.setText(text);
    }
}
